import { Component, OnInit, Input } from '@angular/core';
import { ReportService } from '../../../report.service';
import { LazyLoadEvent } from 'primeng/api';
import { DialogService } from 'primeng/primeng';
import { ReportStandardDetailComponent } from './report-standard-detail.component';

@Component({
  selector: 'report-search-list',
  templateUrl: './report-search-list.component.html',
  styleUrls: ['./report-search-list.component.scss']
})
export class ReportSearchListComponent implements OnInit {

  @Input()
  set id(reportId: number) {
    if (!reportId) return;
    //加载表头
    this.conds = [];
    this.records = [];
    this.reportId = reportId;
    //加载统计信息
    this.reportService.loadReport(reportId).subscribe(report => {
      this.report = report;
      this.pageSize = 30;
      //加载统计表头
      this.reportService.loadStatisHeaderColumns(reportId).subscribe(data => {
        this.headers = data.headers;
        this.cols = data.cols;
        this.loadStatisRecords(null);
      });
    });
  }
  get id(): number { return this.reportId; }

  report: any = {};
  reportId: number = 0;
  loading: boolean = false;
  showPaginator: boolean = false;
  pageSize: number = 30;
  totalRecords: number = 0;
  records: any[] = []; //数据记录
  headers: any[] = []; //表头
  cols: any[] = []; //列信息

  //汇总信息
  summaryFlag: boolean = false;
  summaryColspan: number = 0;
  sumaryCols: any[] = [];
  sumaryData: any = {};
  conds: any[] = [];
  constructor(
    private dialogService: DialogService,
    private reportService: ReportService) { }

  ngOnInit() {
    this.reportService.routeMsgPush$.subscribe(conds => {
      this.conds = conds;
      this.loadStatisRecords(this.conds);
    });
  }

  /**查询数据 */
  loadStatisRecords(conds) {
    this.loading = true;
    if (conds) {
      this.conds = conds;
    }
    this.reportService.loadStatisRecords(this.conds, this.reportId, this.pageSize).subscribe(page => {
      this.loading = false;
      this.records = page.list;
      this.totalRecords = page.total;
      this.showPaginator = (page.total > 30);
    });
  }

  /**数据懒加载 */
  loadStatisLazy(event: LazyLoadEvent) {
    this.loading = true;
    let pageNum = (event.first / event.rows + 1) || 1;
    this.reportService.loadStatisRecords(this.conds, this.reportId, this.pageSize, pageNum).subscribe(page => {
      this.loading = false;
      this.records = page.list;
      this.totalRecords = page.total;
    });
  }
  
  // 文件下载
  download() {
    this.reportService.downloadReport(this.reportId, this.conds, this.report.reportName + '.xlsx').subscribe();
  }
}
